/*
 * Copyright (c) 2013-2018 上海汇数数据. All rights reserved.
 * @(#) SystemError.java 2018-08-08 17:12
 */

package cn.jh.common.core.ret;


import cn.jh.common.core.domain.EnumValue;
import cn.jh.common.core.i18n.I18n;

import java.util.Locale;

import static com.google.common.base.Preconditions.checkArgument;


/**
 * @author Fuchun
 * @since 1.0
 */
public enum SystemError implements EnumValue<String> {

    UNKNOWN_ERROR("10000", "errors.unknown_error", "Unknown error"),

    SYSTEM_ERROR("10001", "errors.system_error", "System error"),

    SYSTEM_BUSY("10002", "errors.system_busy", "System busy"),

    ILLEGAL_REQUEST("10003", "errors.illegal_request", "Illegal request"),

    SERVICE_UNAVAILABLE("10004", "errors.service_unavailable", "Service unavailable"),

    REMOTE_SERVICE_ERROR("10005", "errors.remote_service_error", "Remote service error"),

    UNSUPPORTED_MEDIA_TYPE("10006", "errors.unsupported_media_type", "Unsupported media type", 1),

    PARAM_ERROR("10007", "errors.param_error", "Param error"),

    REQUIRED_PARAMS("10008", "errors.required_params", "Required params", 1),

    ILLEGAL_PARAM("10009", "errors.illegal_param", "Illegal param", 3),

    UNSUPPORTED_HTTP_METHOD("10010", "errors.unsupported_http_method", "Unsupported http method", 1),

    REPEAT_SUBMIT("10011", "errors.repeat_submit", "Repeat submit"),

    PERMISSION_DENIED("10012", "errors.permission_denied", "Permission denied"),

    INVALID_PERMISSION("10013", "errors.invalid_permission", "Invalid permission"),

    IP_LIMIT("10014", "errors.ip_limit", "IP limit"),

    IP_REQUESTS_LIMIT("10015", "errors.ip_requests_limit", "IP requests limit"),

    JOB_EXPIRED("10016", "errors.job_expired", "Job expired"),

    LENGTH_OVER_LIMIT("10017", "errors.length_over_limit", "Length over limit"),

    USER_REQUESTS_LIMIT("10018", "errors.user_requests_limit", "User requests limit"),

    USER_REQUESTS_LIMIT2("10019", "errors.user_requests_limit2", "User requests limit2", 1),

    API_NOT_FOUND("10020", "errors.api_not_found", "API not found", 1),

    INNER_PARAM_ERROR("10021", "errors.inner_param_error", "Inner param error"),

    API_DISABLED("10022", "errors.api_disabled", "API disabled"),

    INVALID_USER("10023", "errors.invalid_user", "Invalid user"),

    MISS_APP_ID("10024", "errors.miss_client_id", "Miss app id"),

    RPC_ERROR("10025", "errors.rpc_error", "RPC error"),

    UNSUPPORTED_HTTP_SCHEME("10026", "errors.unsupported_http_scheme", "Unsupported http scheme", 1)
    ;

    /** 消息码（一串数字） */
    final String code;
    /** I18n 消息码 */
    final String i18nCode;
    /** 默认的消息。*/
    final String message;
    final int argSize;

    SystemError(String code, String i18nCode, String message) {
        this(code, i18nCode, message, 0);
    }

    SystemError(String code, String i18nCode, String message, int argSize) {
        this.code = code;
        this.i18nCode = i18nCode;
        this.message = message;
        this.argSize = argSize;
    }

    public String getCode() {
        return code;
    }

    public String getI18nCode() {
        return i18nCode;
    }

    public String getMessage() {
        return message;
    }

    @Override
    public String getValue() {
        return code;
    }

    public int getArgSize() {
        return argSize;
    }

    public String getMessage(Locale locale, Object... params) {
        return I18n.getOrDefault(i18nCode, params, locale, message);
    }

    public OutVO asOut(Locale locale, Object... params) {
        if (argSize > 0) {
            checkArgument(params != null && params.length == argSize,
                    "The params must be not null or empty");
        }
        String description = getMessage(locale, params);
        return new DefaultOutVO(code, message, description);
    }
}
